package com.zhixinglu.spring.boot.mqtt.tio.listener;

import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * MQTT 客户端
 * @apiNote MQTT client Service
 * @author wsq
 */
@Slf4j
@Service
public class MqttClientService {

	@Autowired
	private MqttClientTemplate client;

	/**
	 * 推送消息
	 * @param topic String 消息主题
	 * @param payload String 消息内容
	 * @return boolean 推送结果
	 */
	public boolean publish(String topic, String payload) {
		return client.publish(topic, payload.getBytes(StandardCharsets.UTF_8));
	}

	/**
	 * 推送消息
	 * @param topic String 消息主题
	 * @param payload String 消息内容
	 * @param qos MqttQoS 消息服务质量
	 * @return boolean 推送结果
	 */
	public boolean publish(String topic, String payload, MqttQoS qos) {
		return client.publish(topic, payload.getBytes(StandardCharsets.UTF_8), qos);
	}

}
